package buymyhome.com.util;

import cn.hutool.core.util.BooleanUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author 杨世博
 */
public class DoLock {
    /**
     * 添加锁
     * @param key
     * @return
     */
    public static boolean tryLock(String key, StringRedisTemplate stringRedisTemplate) {
        Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", 10, TimeUnit.SECONDS);
        return BooleanUtil.isTrue(flag);
    }
    /**
     * 删除锁
     * @param key
     */
    public static void unlock(String key, StringRedisTemplate stringRedisTemplate){
        stringRedisTemplate.delete(key);
    }
}
